//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartElementsView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartElementsView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartElementsView

IMPLEMENT_DYNCREATE(CChartElementsView, CBCGPChartExampleView)

CChartElementsView::CChartElementsView()
	: CBCGPChartExampleView(CChartElementsView::IDD)
{
	//{{AFX_DATA_INIT(CChartElementsView)
	m_bXAxis = TRUE;
	m_bXAxisName = TRUE;
	m_bYAxis = TRUE;
	m_bYAxisName = TRUE;
	m_bDataLabels = TRUE;
	m_bDataMarkers = TRUE;
	m_bLegend = TRUE;
	m_bLegendOverlapsChart = FALSE;
	m_bTitle = TRUE;
	m_bTitleOverlapsChart = FALSE;
	m_bXIntervalInterlasing = TRUE;
	m_bYIntervalInterlasing = FALSE;
	m_bYAxisGridLines = TRUE;
	m_bXAxisGridLines = TRUE;
	m_bSecondSeries = TRUE;
	m_bColoredZones = TRUE;
	m_bCustomLines = TRUE;
	m_bTextObjects = TRUE;
	m_bDataTable = TRUE;
	m_bDataTableLegendKeys = FALSE;
	m_bXAxisReversed = FALSE;
	//}}AFX_DATA_INIT

	m_strokeStyle.SetDashStyle(CBCGPStrokeStyle::BCGP_DASH_STYLE_DASH_DOT);
}

CChartElementsView::~CChartElementsView()
{
}

void CChartElementsView::SetObjectColors()
{
	m_brFill1 = m_bIsDarkBackground ? CBCGPBrush(CBCGPColor::CadetBlue, .5) : CBCGPBrush(CBCGPColor::RoyalBlue, .2);
	m_brFill2 = m_bIsDarkBackground ? CBCGPBrush(CBCGPColor::ForestGreen, .5) : CBCGPBrush(CBCGPColor::Peru, .2);
	m_brFill3 = CBCGPBrush(CBCGPColor::LightSkyBlue, CBCGPColor::White, CBCGPBrush::BCGP_GRADIENT_DIAGONAL_LEFT, m_bIsDarkBackground ? .9 : .4);
	m_brFill4 = CBCGPBrush(CBCGPColor::LightYellow);
	m_brLine1 = CBCGPBrush(CBCGPColor::DarkRed);
	m_brLine2 = CBCGPBrush(CBCGPColor::DeepSkyBlue);
	m_brText1 = CBCGPBrush(CBCGPColor::SteelBlue);
	m_brText2 = CBCGPBrush(CBCGPColor::Brown);

	OnUpdateChart();
}

void CChartElementsView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartElementsView)
	DDX_Control(pDX, IDC_DATA_TABLE_LEGEND_KEYS, m_wndDataTableLegendKeys);
	DDX_Control(pDX, IDC_CHART_AXIS_Y_GRID_LINES, m_wndYAxisGridLines);
	DDX_Control(pDX, IDC_CHART_AXIS_X_GRID_LINES, m_wndXAxisGridLines);
	DDX_Control(pDX, IDC_CHART_AXIS_Y_INTERVAL_INTERLASING, m_wndYAxisInterlasing);
	DDX_Control(pDX, IDC_CHART_AXIS_X_INTERVAL_INTERLASING, m_wndXAxisInterlasing);
	DDX_Control(pDX, IDC_CHART_TITLE_OVERLAPS_CHART, m_wndTitleOverlapsChart);
	DDX_Control(pDX, IDC_CHART_LEGEND_OVERLAPS_CHART, m_wndLegendOverlapsChart);
	DDX_Control(pDX, IDC_CHART_AXIS_Y_NAME, m_wndYAxisName);
	DDX_Control(pDX, IDC_CHART_AXIS_X_NAME, m_wndXAxisName);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_CHART_AXIS_X, m_bXAxis);
	DDX_Check(pDX, IDC_CHART_AXIS_X_NAME, m_bXAxisName);
	DDX_Check(pDX, IDC_CHART_AXIS_Y, m_bYAxis);
	DDX_Check(pDX, IDC_CHART_AXIS_Y_NAME, m_bYAxisName);
	DDX_Check(pDX, IDC_CHART_DATA_LABELS, m_bDataLabels);
	DDX_Check(pDX, IDC_CHART_DATA_MARKERS, m_bDataMarkers);
	DDX_Check(pDX, IDC_CHART_LEGEND, m_bLegend);
	DDX_Check(pDX, IDC_CHART_LEGEND_OVERLAPS_CHART, m_bLegendOverlapsChart);
	DDX_Check(pDX, IDC_CHART_TITLE, m_bTitle);
	DDX_Check(pDX, IDC_CHART_TITLE_OVERLAPS_CHART, m_bTitleOverlapsChart);
	DDX_Check(pDX, IDC_CHART_AXIS_X_INTERVAL_INTERLASING, m_bXIntervalInterlasing);
	DDX_Check(pDX, IDC_CHART_AXIS_Y_INTERVAL_INTERLASING, m_bYIntervalInterlasing);
	DDX_Check(pDX, IDC_CHART_AXIS_Y_GRID_LINES, m_bYAxisGridLines);
	DDX_Check(pDX, IDC_CHART_AXIS_X_GRID_LINES, m_bXAxisGridLines);
	DDX_Check(pDX, IDC_CHART_SECOND_SERIES, m_bSecondSeries);
	DDX_Check(pDX, IDC_CHART_COLORED_ZONES, m_bColoredZones);
	DDX_Check(pDX, IDC_CHART_CUSTOM_LINES, m_bCustomLines);
	DDX_Check(pDX, IDC_CHART_TEXT_OBJECTS, m_bTextObjects);
	DDX_Check(pDX, IDC_DATA_TABLE, m_bDataTable);
	DDX_Check(pDX, IDC_DATA_TABLE_LEGEND_KEYS, m_bDataTableLegendKeys);
	DDX_Check(pDX, IDC_X_AXIS_REVERSED, m_bXAxisReversed);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartElementsView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartElementsView)
	ON_BN_CLICKED(IDC_CHART_TITLE, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_TITLE_OVERLAPS_CHART, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_AXIS_X, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_AXIS_X_NAME, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_AXIS_Y, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_AXIS_Y_NAME, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_DATA_LABELS, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_DATA_MARKERS, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_LEGEND, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_LEGEND_OVERLAPS_CHART, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_AXIS_X_INTERVAL_INTERLASING, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_AXIS_Y_INTERVAL_INTERLASING, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_AXIS_X_GRID_LINES, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_AXIS_Y_GRID_LINES, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_SECOND_SERIES, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_COLORED_ZONES, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_CUSTOM_LINES, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_TEXT_OBJECTS, OnUpdateChart)
	ON_BN_CLICKED(IDC_DATA_TABLE, OnUpdateChart)
	ON_BN_CLICKED(IDC_DATA_TABLE_LEGEND_KEYS, OnUpdateChart)
	ON_BN_CLICKED(IDC_X_AXIS_REVERSED, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartElementsView diagnostics

#ifdef _DEBUG
void CChartElementsView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartElementsView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartElementsView message handlers

void CChartElementsView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;
	
	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartTitle(_T("Chart Title"));

	CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("Series 1"));
	pSeries1->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE);

	CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("Series 2"));
	pSeries2->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE_HERMITE);

	pSeries1->AddDataPoint(0.);
	pSeries1->AddDataPoint(2.);
	pSeries1->AddDataPoint(5.);
	pSeries1->AddDataPoint(3.);
	pSeries1->AddDataPoint(3.5);
	pSeries1->AddDataPoint(5.);
	pSeries1->AddDataPoint(8.);
	pSeries1->AddDataPoint(7.);
	
	pSeries2->AddDataPoint(2.);
	pSeries2->AddDataPoint(4.);
	pSeries2->AddDataPoint(6.);
	pSeries2->AddDataPoint(7.);
	pSeries2->AddDataPoint(6.);
	pSeries2->AddDataPoint(3.);
	pSeries2->AddDataPoint(1.);
	pSeries2->AddDataPoint(0.);

	SetDefaultLineWidth();
	SetObjectColors();

	pChart->SetSeriesShadow();

	OnUpdateChart();
}

void CChartElementsView::OnUpdateChart() 
{
	UpdateData();

	m_wndTitleOverlapsChart.EnableWindow(m_bTitle);
	m_wndLegendOverlapsChart.EnableWindow(m_bLegend);
	m_wndXAxisName.EnableWindow(m_bXAxis && !m_bDataTable);
	m_wndYAxisName.EnableWindow(m_bYAxis);
	m_wndXAxisInterlasing.EnableWindow(m_bXAxis);
	m_wndYAxisInterlasing.EnableWindow(m_bYAxis);
	m_wndXAxisGridLines.EnableWindow(m_bXAxis);
	m_wndYAxisGridLines.EnableWindow(m_bYAxis);
	m_wndDataTableLegendKeys.EnableWindow(m_bDataTable);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowChartTitle(m_bTitle, m_bTitleOverlapsChart);

	BCGPChartFormatDataTable formatDataTable;
	formatDataTable.m_bShowLegendKeys = m_bDataTableLegendKeys;
	formatDataTable.m_bInterlaceRows = TRUE;

	pChart->ShowDataTable(m_bDataTable, &formatDataTable);

	pChart->SetLegendPosition(m_bLegend ? BCGPChartLayout::LP_RIGHT : BCGPChartLayout::LP_NONE, m_bLegendOverlapsChart);

	pChart->ShowAxis(BCGP_CHART_X_PRIMARY_AXIS, m_bXAxis);
	pChart->ShowAxis(BCGP_CHART_Y_PRIMARY_AXIS, m_bYAxis);

	pChart->SetAxisName(BCGP_CHART_X_PRIMARY_AXIS, _T("X Axis"));
	pChart->ShowAxisName(BCGP_CHART_X_PRIMARY_AXIS, m_bXAxisName && !m_bDataTable);
	pChart->ShowAxisIntervalInterlacing(BCGP_CHART_X_PRIMARY_AXIS, m_bXIntervalInterlasing);
	pChart->ShowAxisGridLines(BCGP_CHART_X_PRIMARY_AXIS, m_bXAxisGridLines, FALSE);
	
	pChart->SetAxisName(BCGP_CHART_Y_PRIMARY_AXIS, _T("Y Axis"));
	pChart->ShowAxisName(BCGP_CHART_Y_PRIMARY_AXIS, m_bYAxisName);
	pChart->ShowAxisGridLines(BCGP_CHART_Y_PRIMARY_AXIS, m_bYAxisGridLines, FALSE);
	pChart->ShowAxisIntervalInterlacing(BCGP_CHART_Y_PRIMARY_AXIS, m_bYIntervalInterlasing);

	pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS)->m_bReverseOrder = m_bXAxisReversed;

	pChart->ShowDataMarkers(m_bDataMarkers);
	pChart->ShowDataLabels(m_bDataLabels);

	pChart->ShowSeries(m_bSecondSeries, 1);

	pChart->RemoveAllChartObjects();

	if (m_bColoredZones)
	{
		pChart->AddChartRangeObject(2, 6, TRUE /* Horizontal */, m_brFill1);
		pChart->AddChartRangeObject(6, 7.5, FALSE /* Vertical */, m_brFill2);
	}

	if (m_bCustomLines)
	{
		pChart->AddChartLineObject(5.0, TRUE /* Horizontal */, m_brLine1, 2, &m_strokeStyle);
		pChart->AddChartLineObject(6.5, FALSE /* Vertical */, m_brLine2, 2, &m_strokeStyle);
	}

	if (m_bTextObjects)
	{
		pChart->AddChartObject(new CBCGPChartTextObject(pChart, _T("BCGSoft Chart\nText Object 1"), 1, 8.5, m_brText1, m_brFill3, m_brText1));

		CBCGPChartTextObject* pTextObject2 = new CBCGPChartTextObject
			(pChart, _T("Text Object 2"), 4, 3, m_brText2, m_brFill4, m_brText2, 50, 135 /* Angle */, TRUE);
		pChart->AddChartObject(pTextObject2);
	}

	pChart->Redraw();
}

void CChartElementsView::OnChartAnimation()
{
	CBCGPChartVisualObject* pChart = GetChart();
	if (pChart != NULL)
	{
		CBCGPChartSeries::BCGPChartAnimationStyle style = theApp.m_animationStyle;

		if (m_bXAxisReversed && 
			(style == CBCGPChartSeries::BCGPChartAnimationStyle_Slide || style == CBCGPChartSeries::BCGPChartAnimationStyle_Default))
		{
			style = CBCGPChartSeries::BCGPChartAnimationStyle_SlideReversed;
		}

		pChart->StartAnimation(theApp.m_dblChartAnimationTime, theApp.m_animationType, style);
	}
}
